All files / src/pages/[year] index.astro

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                               
---
import { getCollection } from 'astro:content';
import Layout from '../../layouts/Layout.astro';
import Sidebar from '../../components/Sidebar/index.tsx';
import config from '../../config/index.json';
 
export async function getStaticPaths() {
  const allPosts = await getCollection('blog');
  const sorted = allPosts.sort(
    (a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime()
  );
 
  const years = new Set<string>();
  allPosts.forEach((post) => {
    const year = new Date(post.data.date).getFullYear().toString();
    years.add(year);
  });
 
  return Array.from(years).map((year) => ({
    params: { year },
    props: {
      year,
      posts: sorted.filter(
        (p) => new Date(p.data.date).getFullYear().toString() === year
      ),
      allPosts: sorted,
    },
  }));
}
 
const { year, posts, allPosts } = Astro.props;
 
const title = `${year}年の記事 (${posts.length}件)`;
 
const latestPosts = allPosts.slice(0, 6).map((p) => ({
  title: p.data.title,
  slug: p.slug,
  date: p.data.date.toISOString(),
}));
 
const allPostsForSidebar = allPosts.map((p) => ({
  date: p.data.date.toISOString(),
  tags: p.data.tags || [],
}));
---
 
<Layout
  title={title}
  description={title}
  canonicalUrl={`${config.siteUrl}/${year}/`}
  noindex={true}
>
  <div class="container">
    <div class="row">
      <Sidebar
        client:load
        latestPosts={latestPosts}
        allPosts={allPostsForSidebar}
        totalCount={allPosts.length}
      />
      <div class="col order-2" style="padding:2rem;">
        <h2>{title}</h2>
        <ul>
          {posts.map((post) => {
            const url = post.slug;
            const dateStr = new Date(post.data.date).toISOString().split('T')[0];
            return (
              <li>
                {dateStr}&nbsp;&nbsp;
                <a href={`/${url}/`}>{post.data.title}</a>
              </li>
            );
          })}
        </ul>
      </div>
      <div class="col-xl-2 col-lg-1 order-3"></div>
    </div>
  </div>
</Layout>